package com.example.mvp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

//import com.example.mvp.binder.BaseAdapterDataBinder;
import com.example.mvp.binder.BaseDataBinder;
//import com.example.mvp.delegate.ADelegate;
import com.example.mvp.delegate.XDelegate;

import java.util.ArrayList;
import java.util.List;


/**
 * MVP模式的公共适配器基类
 * Created by sun.li on 2018/6/6.
 *
 * @author sun.li
 */

public abstract class BaseLibAdapter<T extends XDelegate,D extends Object> extends android.widget.BaseAdapter {

//    protected BaseAdapterDataBinder binder;

    private List<D> data;

    private Context context;

    public BaseLibAdapter(Context context, List<D> data) {
        this.context = context;
        if (null != data) {
            this.data = data;
        } else {
            this.data = new ArrayList<>();
        }
    }

    @Override
    public int getCount() {
        if (null == data) {
            return 0;
        }
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        if (null == data) {
            return null;
        }
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        T viewDelegate = null;
        if (null == convertView) {
            viewDelegate = initDelegate(context);
            if (null != viewDelegate) {
                convertView = viewDelegate.getRootView();
                convertView.setTag(viewDelegate);
            }
        }else{
            viewDelegate = (T) convertView.getTag();
        }
        try {
            if (getDataBinder() != null) {
                getDataBinder().viewBindModel(viewDelegate, getItem(position));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        notifyView(viewDelegate, position);
        bindEvenListener(viewDelegate, position);
        return convertView;
    }

    /** 注册界面控件点击事件*/
    protected abstract void bindEvenListener(T viewDelegate, int position);

    /** 自定义实现View刷新操作，通常用于单个的刷新或者未进行数据绑定时使用，如已经进行过数据绑定，则无需具体实现此方法*/
    protected abstract void notifyView(T viewDelegate, int position);

    /** 指定界面的视图控制器，必须指定*/
    public abstract Class<T> getDelegateClass();

    /** 公共数据视图绑定控制器数据对象*/
    public abstract BaseDataBinder getDataBinder();

    public List<D> getData() {
        return data;
    }

    public void setData(List<D> data) {
        this.data = data;
    }

    public Context getContext() {
        return context;
    }

    /** 初始化视图托管类对象*/
    public T initDelegate(Context context){
        T viewDelegate;
        try {
            viewDelegate = getDelegateClass().newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException("create IDelegate error");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("create IDelegate error");
        }
        if(null!=context){
            viewDelegate.create(LayoutInflater.from(context),null,null);
        }
//        if(null == binder){
//            binder = getDataBinder();
//        }
        return viewDelegate;
    }

//    @Override
//    public void notifyDataSetChanged() {
//        super.notifyDataSetChanged();
//    }

}
